

#include  <msp430x54xA.h>
#include  "Drivers.h"
#include  "..\ESS_CommonDefine.h"

extern unsigned char G_LowPowerMode0Flag;
extern unsigned char G_SystemModeStatus;  //Show ESS System current mode
extern unsigned char G_ChangeModeFlag;  //

////////////////////////////////////////////////////////////////////////////
//  Button configuration
//  Switch P2.3, press = high, releae = low
////////////////////////////////////////////////////////////////////////////
void InitButton(){
  // Set up pins used by IO unit
  BUTTON_PORT_DIR &= ~SW1_BUTTON;
  BUTTON_PORT_SEL &= ~SW1_BUTTON;
}

void Enable_SW_Interrupt(unsigned char enable){
  if(enable){
    ///////////////////////////////////////////////////////////////////////
    // for demo board
    //P2REN |= SW1_BUTTON;                            // Enable port internal resistance
    //P2OUT |= SW1_BUTTON;                            // Set port as pull-Up resistance
    //BUTTON_PORT_IES |= SW1_BUTTON;     // set button trigger Hi to Lo edge
    ///////////////////////////////////////////////////////////////////////
    BUTTON_PORT_IES &= ~SW1_BUTTON;    // set button trigger Lo to Hi edge
    BUTTON_PORT_IFG &= ~SW1_BUTTON;    // set button IFG cleared 
    
    BUTTON_PORT_IE |= SW1_BUTTON;      // set button interrupt enabled
  }else{
    BUTTON_PORT_IE &= ~SW1_BUTTON;      // set button interrupt disabled
  }
}

unsigned char getSW1Status(){
  return ((BUTTON_PORT_IN & SW1_BUTTON)== 0)? false : true;
}


// Port  interrupt service routine
#pragma vector=BUTTON_PORT_VECTOR
__interrupt void Port(void)
{
  if( G_SystemModeStatus == AssemblyModeReady){
    G_ChangeModeFlag = true;
  }
  
  
  if(G_LowPowerMode0Flag){
    Enable_SW_Interrupt(false);
    __bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
    G_LowPowerMode0Flag = false;
  }
  BUTTON_PORT_IFG &= ~SW1_BUTTON;    // set button IFG cleared
  
  if(G_LowPowerMode0Flag){
    setTimerInterrupt(DeviceOn);
    Enable_SW_Interrupt(false);
  }
  
  if(getSW1Status()){
    //showLEDs(0x0038);
    //P1OUT ^= 0x03;                            // P1.0 = toggle
    //__delay_cycles(52600);                     // Delay required between transaction
    //setButtonTimerToRunFun(&LLLD,  100);
  }

}
